package com.syb.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import mybatis.com.syb.dao.MyBatisResourcesDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.ranges.RangeException;

import com.syb.dao.ResourcesDao;
import com.syb.dao.RoleDao;
import com.syb.entity.function.SysRole;
import com.syb.service.RoleService;
import com.syb.util.PageResults;

@Service
public class RoleServiceImpl implements RoleService{
	
	@Autowired
	private RoleDao roleDao;

	@Autowired
	private MyBatisResourcesDao myBatisResourcesDao;
	
	@Autowired
	private ResourcesDao resourcesDao;

	public PageResults<SysRole> findSysRoleList(PageResults<SysRole> page) throws Exception {
		PageResults<SysRole> sysRolePageList = new PageResults<SysRole>();
		List<SysRole> sysRoleList = new ArrayList<SysRole>();
		int count = 0;
		try{
			sysRoleList = roleDao.findSysRoleList(page);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		try{
			count = roleDao.findSysRoleCount(page);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		sysRolePageList.setPageNo((page.getCurrentPage()/page.getPageSize())+1);
		sysRolePageList.setResults(sysRoleList);
		sysRolePageList.setTotalCount(count);
		return sysRolePageList;
	}

	@Override
	public List<Map<String,Object>> findSysRoleResources(Integer roleId)throws Exception {
		//查询出所有的资源
		List<Map<String,Object>> sysResourcesList = new ArrayList<Map<String,Object>>();
		//用来放父菜单
		List<Map<String,Object>> dingMap = new ArrayList<Map<String,Object>>();;
		//子菜单
		List<Map<String,Object>> ziMap= new ArrayList<Map<String,Object>>();
		try{
			sysResourcesList = myBatisResourcesDao.findSysResouceList(roleId);
		}catch(Exception e){
			throw new Exception("查询资源失败！");
		}
		if(sysResourcesList.size()>0){
			for(int i=0;i<sysResourcesList.size();i++){
				//看看是否是父类
				if(sysResourcesList.get(i).get("text")!=null &&!("").equals(sysResourcesList.get(i).get("text"))){
					 dingMap.add(sysResourcesList.get(i));
				}
				
			}
			
			for(int i=0;i<sysResourcesList.size();i++){
				if(dingMap.size()>0){
					if(sysResourcesList.get(i).get("parentId")!=null &&!("").equals(sysResourcesList.get(i).get("parentId")) )
					for(int j=0;j<dingMap.size();j++){
						if(((Map)dingMap.get(j)).get("id").equals(sysResourcesList.get(i).get("parentId"))){
							ziMap.add(sysResourcesList.get(i));
							((Map)dingMap.get(j)).put("parten", ziMap);
						}
						
					}
				}
			}
		}
		//查询出角色下的资源
		return dingMap;
	}

	/**
	 * 更新资源
	 */
	@Override
	public void updateRoleResourcesList(List<Map<String,Object>> list) throws Exception {
		Integer id = null;
		if(list.size()>0){
			id = Integer.parseInt(list.get(0).get("id").toString());
		}
		if(id!=null){
			try{
			myBatisResourcesDao.delResourceList(id);
			}catch(Exception e){
				throw new RuntimeException("删除失败！");
			}
		}
		if(list.size()>0&&list.get(0).containsKey("resourceId")){
			try{
				myBatisResourcesDao.insertResourceList(list);
			}catch(Exception e){
				throw new RuntimeException("添加失败！");
			}
		}
		
	}

	/**
	 * 根据id查看角色详情
	 */
	@Override
	public SysRole findSysRoleById(Integer id) throws Exception {
		SysRole sys = new SysRole();
		try{
			sys = roleDao.findSysRoleById(id);
		}catch(Exception e){
			throw new Exception("查询失败！");
		}
		return sys;
	}

	/**
	 * 更新角色
	 */
	@Override
	public void updateRole(SysRole syr) throws Exception {
		//根据角色名查询看是否已存在
		List<Map<String,Object>> list= new ArrayList<Map<String,Object>>();
		try{
			list = myBatisResourcesDao.findSysRoleById(syr.getId());
		}catch(Exception e){
			throw new Exception("查询失败！");
		}
		if(list.size()<=0){
			throw new Exception("数据不存在！");
		}
		if(!list.get(0).get("role_name").equals(syr.getRoleName())){
			try{
				list = myBatisResourcesDao.findSysRoleByName(syr.getRoleName());
			}catch(Exception e){
				throw new Exception("查询失败！");
			}
			
			if(list.size()>0){
				throw new Exception("角色名称已存在！");
			}
		}
		
		try{
			roleDao.updateRole(syr);
		}catch(Exception e){
			throw new Exception("更新失败！");
		}
		
	}

	/**
	 * 新增角色
	 */
	@Override
	public void insertRole(SysRole syr) throws Exception {
		List<Map<String,Object>> list= new ArrayList<Map<String,Object>>();
		try{
			list = myBatisResourcesDao.findSysRoleByName(syr.getRoleName());
		}catch(Exception e){
			throw new Exception("查询失败！");
		}
		
		if(list.size()>0){
			throw new Exception("角色名称已存在！");
		}
		
		try{
			roleDao.updateRole(syr);
		}catch(Exception e){
			throw new Exception("新增失败！");
		}
	}

	/**
	 * 删除角色
	 */
	@Override
	public void deleteRole(Integer id) throws Exception {
		try{
		myBatisResourcesDao.delResourceList(id);
		}catch(Exception e){
			throw new RuntimeException("删除关联表失败！");
		}
		try{
		myBatisResourcesDao.deleteRole(id);
		}catch(Exception e){
			throw new RuntimeException("删除角色失败！");
		}
	}
	
	

}
